package GUI.Cocina;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Button;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.IngredientVO;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.TableColumn;

public class IngredienteFaltante extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Label lblSeleccioneLosIngredientes;
	private Table table_1;
	private TableItem tableItem;
	private TableColumn tblclmnNewColumn;
	private TableColumn tblclmnNewColumn_1;
	private Button btnNewButton;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public IngredienteFaltante(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(794, 493);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		Label lblListaDeIngredientes = new Label(shell, SWT.NONE);
		FormData fd_lblListaDeIngredientes = new FormData();
		fd_lblListaDeIngredientes.left = new FormAttachment(0, 87);
		lblListaDeIngredientes.setLayoutData(fd_lblListaDeIngredientes);
		lblListaDeIngredientes.setText("Lista de ingredientes");
		
		Label lblIngredientesFaltantes = new Label(shell, SWT.NONE);
		fd_lblListaDeIngredientes.right = new FormAttachment(100, -337);
		FormData fd_lblIngredientesFaltantes = new FormData();
		fd_lblIngredientesFaltantes.top = new FormAttachment(lblListaDeIngredientes, 0, SWT.TOP);
		fd_lblIngredientesFaltantes.left = new FormAttachment(lblListaDeIngredientes, 78);
		lblIngredientesFaltantes.setLayoutData(fd_lblIngredientesFaltantes);
		lblIngredientesFaltantes.setText("Ingredientes Faltantes");
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.top = new FormAttachment(lblListaDeIngredientes, 18);
		fd_table.left = new FormAttachment(0, 27);
		fd_table.bottom = new FormAttachment(100, -39);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		fd_table.right = new FormAttachment(100, -502);
		
		lblSeleccioneLosIngredientes = new Label(shell, SWT.NONE);
		fd_lblListaDeIngredientes.top = new FormAttachment(lblSeleccioneLosIngredientes, 22);
		FormData fd_lblSeleccioneLosIngredientes = new FormData();
		fd_lblSeleccioneLosIngredientes.top = new FormAttachment(0, 22);
		fd_lblSeleccioneLosIngredientes.left = new FormAttachment(0, 240);
		lblSeleccioneLosIngredientes.setLayoutData(fd_lblSeleccioneLosIngredientes);
		lblSeleccioneLosIngredientes.setText("Seleccione los Ingredientes Faltanes");
		
		Button falta = new Button(shell, SWT.NONE);
		falta.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem t = table.getSelection()[0];
				TableItem t1 = new TableItem(table_1, SWT.NONE);
				t1.setText(0, t.getText());
				table.remove(table.getSelectionIndex());
				
				
				
			}
		});
		FormData fd_falta = new FormData();
		fd_falta.top = new FormAttachment(lblListaDeIngredientes, 46);
		fd_falta.left = new FormAttachment(table, 36);
		falta.setLayoutData(fd_falta);
		falta.setText("Agregar a Faltantes");
		
		Button noFalta = new Button(shell, SWT.NONE);
		noFalta.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem t = table_1.getSelection()[0];
				table_1.remove(table_1.getSelectionIndex());
				TableItem tt = new TableItem(table, SWT.NONE);
				tt.setText(t.getText());
			}
		});
		FormData fd_noFalta = new FormData();
		fd_noFalta.right = new FormAttachment(lblListaDeIngredientes, 0, SWT.RIGHT);
		fd_noFalta.top = new FormAttachment(falta, 86);
		fd_noFalta.left = new FormAttachment(falta, 0, SWT.LEFT);
		noFalta.setLayoutData(fd_noFalta);
		noFalta.setText("Quitar de la Lista");
		
		table_1 = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table_1.setLinesVisible(true);
		table_1.setHeaderVisible(true);
		FormData fd_table_1 = new FormData();
		fd_table_1.left = new FormAttachment(falta, 40);
		fd_table_1.right = new FormAttachment(falta, 305, SWT.RIGHT);
		fd_table_1.top = new FormAttachment(table, 0, SWT.TOP);
		fd_table_1.bottom = new FormAttachment(table, 0, SWT.BOTTOM);
		
	
		tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(264);
		tblclmnNewColumn.setText("");
		table_1.setLayoutData(fd_table_1);
		
		tblclmnNewColumn_1 = new TableColumn(table_1, SWT.NONE);
		tblclmnNewColumn_1.setWidth(264);
		tblclmnNewColumn_1.setText("");
		
		btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] faltan = table_1.getItems();
				IngredientVO [] ings= null;
				ArticleProxyMgt o = ServiceFacade.getInstance().getArticleProxyMgt();
				try {
					ings= o.getAllIngredients();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				ArrayList<IngredientVO> lista = new ArrayList<IngredientVO>();
				for(int i=0;i<faltan.length;i++){
					for (int j=0;j<ings.length;j++){
						if(faltan[i].getText().equalsIgnoreCase(ings[j].getIngr())){
							lista.add(ings[j]);
							
						}
					}
					
				}
				try {
					o.addOutOfStock(lista);
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ProxyException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				shell.setVisible(false);
			}
		});
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.right = new FormAttachment(lblListaDeIngredientes, 0, SWT.RIGHT);
		fd_btnNewButton.top = new FormAttachment(noFalta, 64);
		fd_btnNewButton.left = new FormAttachment(falta, 0, SWT.LEFT);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("INGRESAR");
		
		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		IngredientVO[] ings=null;
		try {
			ings = uso.getInStockIngredients();
		} catch (RemoteException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		} catch (ProxyException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		}
		for(int i=0;i<ings.length;i++){
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(ings[i].getIngr());
			table_1.setLayoutData(fd_table_1);
		}
		

	}
}
